/*
 * ProjectDataModel.java
 *
 * Create on Mar 28, 2014 Error: on line 6, column 42 in Templates/Licenses/license-myLicence10.txt
 The string doesn't match the expected date/time format. The string to parse was: "Mar 28, 2014". The expected format was: "h:mm:ss a".
 *
 * Copyright (c) 2014 Zhiyong's Design Studio.
 *
 * This software contains confidential proprietary information belonging
 * to Zhiyong's Design Studio. No part of this information may be used, reproduced, 
 * or stored without prior written consent of Zhiyong's Design Studio.
 *
 */
package com.zhiyongstudio.contactorbook.datamodel;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * @version Revision 1.0.0.0
 * @since Build 1.0.0.0: Mar 28, 2014
 * @author Zhiyong's Design Studio
 */
public class ProjectDataModel {

    private LinkedList linkedCalleeList;
    private File file;

    public ProjectDataModel(File file) {
        this.file = file;
        linkedCalleeList = new LinkedList();
        createCalleeList(file, linkedCalleeList);
    }

    private void createCalleeList(File file, LinkedList linkedCalleeList) {
        String temp;
        String userName = "";
        String passWord = "";
        String[] split;
        try {
            Scanner scanner = new Scanner(file);
            while (scanner.hasNext()) {
                temp = scanner.next();
                if (temp.startsWith("[") && temp.endsWith("]")) {
                    if (!temp.contains("general")) {
                        userName = temp.substring(1, temp.length() - 1);
                    }
                } else if (temp.startsWith("password=")) {
                    passWord = temp.substring(9, temp.length());
                    linkedCalleeList.add(new ProjectDataNode(userName, passWord));
                }
            }
            sortCalleeList();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ProjectDataModel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void deleteCallee(int delIndex) {
        linkedCalleeList.remove(delIndex);
    }

    public boolean setCallee(String userName, String passWord) {
        boolean isSet = false;

        if (findCallee(userName) == -1) {
            addCallee(new ProjectDataNode(passWord, passWord));
        } else {
            modifyNode(findCallee(userName), userName, passWord);
        }
        return isSet;
    }

    /**
     * It's a binary search method for a callee node.
     * @param userName String user name of the callee node. 
     * @return int -1:not found; <br>and the integer larger and equal than zero represents the position of the node in the linked list.
     */
    public int findCallee(String userName) {
        int found = -1;
      int userNameNr = Integer.parseInt(userName);
      int low = 1;
      int high = linkedCalleeList.size()-1;
      int mid;
      
      while(low<=high){
          mid = (low+high)/2;
          int tmp = Integer.parseInt(((ProjectDataNode)linkedCalleeList.get(mid)).getCallNr());
          if(userNameNr<tmp){
              high = mid-1;
          }else if(userNameNr>tmp){
              low = mid+1;
          }else if(tmp == userNameNr){
              found = mid;
              break;
          }
      }        
        return found;
    }

    private void modifyNode(int index, String userName, String passWord) {
        linkedCalleeList.set(index, new ProjectDataNode(userName, passWord));
    }

    public void addCallee(ProjectDataNode node) {
        if (findCallee(node.getCallNr()) == -1) {
            linkedCalleeList.add(node);
        } else {
            modifyNode(findCallee(node.getCallNr()), node.getCallNr(), node.getPassWord());
        }
       // sortCalleeList(linkedCalleeList);
    }

    public void sortCalleeList() {
        ProjectDataNode callCenter = (ProjectDataNode) linkedCalleeList.getFirst();
        linkedCalleeList.remove();
        Collections.sort(linkedCalleeList, new LessThanComparator());
        linkedCalleeList.addFirst(callCenter);
    }
    
    public int getSize(){
        return linkedCalleeList.size();
    }
    
    public LinkedList getLinkedList(){
        return this.linkedCalleeList;
    }

}
